package lormi.meipin.controller.api;


import lormi.meipin.annotation.NotValidUser;
import lormi.meipin.common.Result;
import lormi.meipin.controller.BaseController;
import lormi.meipin.exception.LMException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * 美聘城市信息
 * */
@RestController
@RequestMapping(value="v2/city/",method=RequestMethod.POST,produces = "application/json; charset=utf-8")
public class CityController extends BaseController{

	/**
	 * 获取所有省份(level=province)
	 **/
	@NotValidUser
	@RequestMapping("provinces")
	public String getProvinces() throws LMException{
		return Result.success(cityService.getAllProvinces());
	}
	/**
	 * @throws UnsupportedEncodingException 
	 * 获取地区信息通过地区名
	 * @param name 省份或者城市名称
	 **/
	@NotValidUser
	@RequestMapping("name")
	@Valid
	public String selectByCityName(@NotNull(message="100033") @RequestParam(value="name",required=false) String name )
			throws LMException, UnsupportedEncodingException{
		
		return Result.success(cityService.getByCityName(URLDecoder.decode(name,"utf-8")));
	}
	/**
	 * 获取一个省下的所有城市(level=city)
	 * @Param provinceid 省份ID
	 **/
	@NotValidUser
	@RequestMapping("citys")
	@Valid
	public String selectCityById(@NotNull(message="100033") @RequestParam(value="provinceid",required=false) String provinceid)
			throws LMException{
		
		return Result.success(cityService.getAllCityByPrivince(provinceid));
	}
	/**
	 * 获取一个城市下的所有区(level=district)
	 * @Param cityid  城市ID
	 * */
	@NotValidUser
	@RequestMapping("districts")
	@Valid
	public String selectDistrictByCityId(@NotNull(message="100033") @RequestParam(value="cityid",required=false) String cityid) throws LMException{
		
		return Result.success(cityService.getAllDistrictByCityId(cityid));
	}
	
	
	/**
	 * 获取所有城市(按首字母排序)
	 * */
	@NotValidUser
	@RequestMapping("all")
	public String cityAll() throws LMException{
		
		return Result.success(cityService.getAllCitySortAZ());
	}
	
	/**
	 * 获取所有城市(按首字母排序)
	 * */
	@NotValidUser
	@RequestMapping("aller")
	public String cityAller() throws LMException{
		
		return Result.success(cityService.getAllerCitySortAZ());
	}
	
	/**
	 * 获取热门城市
	 **/
	@NotValidUser
	@RequestMapping("hotcity")
	public String hotcity() throws LMException{

		return Result.success(cityService.getHotCity());
	}
	
}
